package com.zdxc.service;

import java.util.List;
import java.util.Map;

import com.zdxc.entities.Collectdata;
import com.zdxc.vo.CollectDataVO;
import common.dao.BaseMyBatisDAO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import common.bean.Paging;

/**
 * 数据采集表管理模块Service接口实现
 *
 * @author liuxj
 * @version 1.0.0 2017-08-08
 */
@Lazy
@Service
public class CollectDataService   {
    /** SQL的命名空间 */
    @Autowired
    BaseMyBatisDAO baseMyBatisDAO;
    private static final String SQL_NS = "com.zdxc.dao.Collectdata.";

    /**
     * 根据分页条件查询数据采集表数据并分页
     *
     * @param collectDataVO
     *            查询参数
     * @param paging
     *            分页参数
     * @return 分页数据
     */
    public Paging findCollectDataByCondition(CollectDataVO collectDataVO, Paging paging) {
        paging.setStringParams(collectDataVO);

        paging = baseMyBatisDAO.findForPageHelper(SQL_NS + "findCollectDataByCondition", paging);

        return paging;
    }

    /**
     * 根据分页条件查询数据采集表数据并分页(用于直接显示采集信息)
     *
     * @param collectDataVO
     *            查询参数
     * @param paging
     *            分页参数
     * @return 分页数据
     */
    public Paging findCollectDataByCondition2(CollectDataVO collectDataVO, Paging paging) {
        paging.setStringParams(collectDataVO);

        paging =  baseMyBatisDAO.findForPageHelper(SQL_NS + "findCollectDataByCondition2", paging);

        return paging;
    }
    /**
     * 外部接口取数据
     * @param map
     * @return
     */
    public List<CollectDataVO>  findCDRemoteByCondition(Map<String ,Object> map) {
        List<CollectDataVO>  list =  baseMyBatisDAO.findForList(SQL_NS + "findCollectDataRemoteByCondition", map);
        return list;
    }

    /**
     * 新增数据采集表
     *
     * @param collectDataEntity
     *            数据采集表
     */
    public void addCollectData(Collectdata collectDataEntity) {
        collectDataEntity.setSid(common.util.StringUtils.getUUID());
        // baseJpaDAO.persist(collectDataEntity);

    }

    /**
     * 新增数据采集表
     *
     * @param idList
     *            数据采集表
     */
    public void addCollectDataByRelaId(List<String> idList) {

        baseMyBatisDAO.insert(SQL_NS + "insertCollectDataByRelaId", idList);
    }
    /**
     * 修改数据采集表
     *
     * @param collectDataEntity
     *            数据采集表
     */
    public void modifyCollectData(Collectdata collectDataEntity) {
        baseMyBatisDAO.update(SQL_NS + "update", collectDataEntity);
    }

    /**
     * 批量删除数据采集表
     *
     * @param idList
     *            ID列表
     */
    public void deleteCollectDataByIds(List<String> idList) {
        baseMyBatisDAO.delete(SQL_NS + "deleteCollectDataByIds", idList);
    }

    /**
     * 批量删除数据采集表
     *
     * @param idList
     *            taskID列表
     */
    public void deleteCollectDataByTaskIds(List<String> idList) {
        baseMyBatisDAO.delete(SQL_NS + "deleteCollectDataByTaskIds", idList);
    }

    /**
     * 批量删除数据采集表
     *
     * @param idList
     *            taskID列表
     */
    public void deleteCollectDataByPlanIds(List<String> idList) {
        baseMyBatisDAO.delete(SQL_NS + "deleteCollectDataByPlanIds", idList);
    }


    /**
     * 批量删除数据采集表
     *
     * @param idList
     *            ID列表
     */
    public void deleteCollectDataByRelaId(List<String> idList) {
        baseMyBatisDAO.delete(SQL_NS + "deleteCollectDataByRelaIds", idList);
    }

    /**
     * 根据ID查询数据采集表
     *
     * @param dataId
     * @return Collectdata
     */
    public Collectdata findCollectDataById(String dataId){
        return  baseMyBatisDAO.findUnique(SQL_NS+"findByKey", dataId);
    }

    /**
     * ipad数据处理
     * @param collectDataVOList
     */
    public void SetCollectData(List<CollectDataVO> collectDataVOList ) {
        if(CollectionUtils.isNotEmpty(collectDataVOList)){
            for (CollectDataVO collectdatavo : collectDataVOList) {
                String sidjh =  collectdatavo.getDataId();
                if(StringUtils.isNotEmpty(sidjh)){
                    Collectdata  collectDataEntity = findCollectDataById(sidjh);
                    if (collectDataEntity == null) {

                        collectDataEntity = new Collectdata();
                        BeanUtils.copyProperties(collectdatavo, collectDataEntity);

                        collectDataEntity.setRecordStatus(1);
                        collectDataEntity.setDataId(common.util.StringUtils.getUUID());
                        addCollectData(collectDataEntity);
                    } else {
                        BeanUtils.copyProperties(collectdatavo, collectDataEntity);
                        collectDataEntity.setRecordStatus(1);
                        modifyCollectData(collectDataEntity);
                    }
                }else{
                    throw new RuntimeException();
                }
            }
        }

    }
}

